import {addorder, getorder, uporder, delorder} from '../../assets/js/request.js'
import moment from "moment"
const state = {
    orderList: JSON.parse(localStorage.getItem('orderlist')) || []
}
const mutations = {
    // 请获取到的订单列表存进vuex
    assignment(state, data) {
        state.orderList = data.rows.map(item => {
            return {
                ...item, order_time:moment(item.order_time, 'x').format('YYYY-MM-DD HH:mm:ss'),
                createdAt: moment(item.createdAt,"YYYY-MM-DDTHH:mm:ss.SSS").format('YYYY-MM-DD HH:mm:ss'),
                goods_info: JSON.parse(item.goods_info)
            }
        })
    }

}
const actions = {
    // 获取订单列表
    async getOrder({commit}, data) {
        let orderArr = await getorder(data)
        commit("assignment", orderArr)
    },

    // 添加订单请求
    addOrder({commit, dispatch}, data) {
        return addorder(data)
    },

    // 修改订单请求
    async upOrder({commit, dispatch}, data) {
        await uporder(data)
        dispatch("getOrder", {project_id: 18, limit:100})
    },

    // 删除对应订单
    delVuexOrder({commit, dispatch}, id) {
        return delorder(id)
    }

}
const getters = {
    
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}